package cn.iocoder.yudao.module.member.service.userpointrecords;

import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.member.controller.admin.userpointrecords.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userpointrecords.UserPointRecordsDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.userpointrecords.UserPointRecordsMapper;

import java.time.LocalDateTime;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 用户积分流水 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserPointRecordsServiceImpl implements UserPointRecordsService {

    @Resource
    private UserPointRecordsMapper userPointRecordsMapper;
    @Resource
    private MemberUserService memberUserService;

    @Override
    public Long createUserPointRecords(UserPointRecordsSaveReqVO createReqVO) {
        // 插入
        UserPointRecordsDO userPointRecords = BeanUtils.toBean(createReqVO, UserPointRecordsDO.class);
        userPointRecords.setCreateTime(LocalDateTime.now());
        userPointRecordsMapper.insert(userPointRecords);
        // 返回
        return userPointRecords.getId();
    }

    @Override
    public void updateUserPointRecords(UserPointRecordsSaveReqVO updateReqVO) {
        // 校验存在
        validateUserPointRecordsExists(updateReqVO.getId());
        // 更新
        UserPointRecordsDO updateObj = BeanUtils.toBean(updateReqVO, UserPointRecordsDO.class);
        userPointRecordsMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserPointRecords(Long id) {
        // 校验存在
        validateUserPointRecordsExists(id);
        // 删除
        userPointRecordsMapper.deleteById(id);
    }

    private void validateUserPointRecordsExists(Long id) {
        if (userPointRecordsMapper.selectById(id) == null) {
            throw exception(USER_POINT_RECORDS_NOT_EXISTS);
        }
    }

    @Override
    public UserPointRecordsDO getUserPointRecords(Long id) {
        return userPointRecordsMapper.selectById(id);
    }

    @Override
    public PageResult<UserPointRecordsRespVO> getUserPointRecordsPage(UserPointRecordsPageReqVO pageReqVO) {
        if (Objects.nonNull(pageReqVO.getMobile())) {
            pageReqVO.setUserId(0L);
            MemberUserDO memberUserDO = memberUserService.getUserByMobile(pageReqVO.getMobile());
            if (Objects.nonNull(memberUserDO)) {
                pageReqVO.setUserId(memberUserDO.getId());
            }
        } else if (Objects.nonNull(pageReqVO.getInviteCode())) {
            pageReqVO.setUserId(0L);
            MemberUserDO memberUserDO = memberUserService.getUserByInviteCode(pageReqVO.getInviteCode());
            if (Objects.nonNull(memberUserDO)) {
                pageReqVO.setUserId(memberUserDO.getId());
            }
        }
        PageResult<UserPointRecordsDO> pageResult = userPointRecordsMapper.selectPage(pageReqVO);
        PageResult<UserPointRecordsRespVO> recordsRespVOPageResult = BeanUtils.toBean(pageResult, UserPointRecordsRespVO.class);
        return recordsRespVOPageResult;
    }

    @Override
    public Integer useTotalPoints(Long userId) {
        Integer useTotalPoints = userPointRecordsMapper.useTotalPoints(userId);
        return Objects.isNull(useTotalPoints) ? 0 : useTotalPoints;
    }

}